<?php namespace App\Modules\Admin\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
// Models
use App\Models\Role;
// Requests
use App\Modules\Admin\Requests\Roles\AddRoleRequest;
use App\Modules\Admin\Requests\Roles\EditRoleRequest;
use App\Modules\Admin\Requests\Roles\SearchRequest;
use App\Modules\Admin\Controllers\Controller;

/**
 * Controller to handle the roles CRUD in the admin part
 *
 * @author Todor Todorov <todstoychev@gmail.com>
 * @package App\Http\Controllers\Admin
 */
class AdminRolesController extends Controller
{

    /**
     * define action names
     * @var array
     */
    public static $actionNames = [
        'getAll'=>'访问角色列表',
        'postSearch'=>'执行角色搜索',
        'getAdd'=>'访问添加角色',
        'postAdd'=>'执行添加角色',
        'getDelete'=>'执行删除角色',
        'getEdit'=>'访问编辑角色',
        'postEdit'=>'执行编辑角色'
    ];

    /**
     * Get all roles page
     *
     * @param Request $request
     * @return View
     */
    public function getAll(Request $request)
    {
        $search = $request->input('search');
        if(is_null($search)){
            $query = new Role();
        }else{
            $query = Role::search($search);
        }

        return $this->all($request, $query, 'admin/roles', trans('roles.all_title'), trans('roles.delete_message'), 'roles.all');
    }

    /**
     * Handles search
     *
     * @param Request $request
     *
     * @return \Illuminate\View\View
     */
    public function postSearch(SearchRequest $request)
    {
        return $this->getAll(
            $request,
            $request->input('search'),
            null,
            null
        );
    }

    /**
     * Render Add role page
     *
     * @return View
     */
    public function getAdd()
    {
        return $this->render('roles.add');
    }

    /**
     * Handles role creating
     *
     * @param AddRoleRequest $request
     * @return RedirectResponse
     */
    public function postAdd(AddRoleRequest $request)
    {
        Role::create([
            'role' => $request->input('role')
        ]);

        flash()->success(trans('roles.role_added'));

        return redirect()->back();
    }

    /**
     * Delete role
     *
     * @param int $id Role id
     * @return RedirectResponse
     */
    public function getDelete($id)
    {
        $role = Role::find($id);

        try {
            if ($role->role != 'admin') {
                $role->delete();

                flash()->success(trans('roles.role_deleted'));

                return redirect()->back();
            } else {
                throw new \Exception('Can not delete!');
            }
        } catch (\Exception $e) {
            flash()->error(trans('roles.can_not_delete'));

            return redirect()->back();
        }
    }

    /**
     * Get edit role page
     *
     * @param $id
     * @return RedirectResponse|View
     */
    public function getEdit($id)
    {
        $role = Role::find($id);

        if ($role->role != 'admin') {
            return $this->render('roles.edit', ['role' => $role]);
        } else {
            flash()->error(trans('roles.can_not_edit'));

            return redirect()->back();
        }
    }

    /**
     * Handles role update
     *
     * @param EditRoleRequest $request
     * @param int $id Role id
     * @return RedirectResponse
     */
    public function putEdit(EditRoleRequest $request, $id)
    {
        $role = Role::find($id);
        $check = Role::checkOnEdit($role->id, ['role' => $request->input('role')]);

        if (!$check) {
            $role->role = $request->input('role');
            $role->save();

            flash()->success(trans('roles.role_edited'));
        } else {
            flash()->error(trans('roles.role_exists'));
        }
        return redirect()->back();
    }

}
